﻿using Microsoft.CognitiveServices.Speech;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 测试语音输入03
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void StartButton_Click(object sender, RoutedEventArgs e)
        {
            await RecognizeSpeechAsync();
        }

        private async Task RecognizeSpeechAsync()
        {
            var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
            using (var recognizer = new SpeechRecognizer(config))
            {
                var result = await recognizer.RecognizeOnceAsync();
                if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    ResultTextBox.Text = result.Text;
                }
                else if (result.Reason == ResultReason.NoMatch)
                {
                    ResultTextBox.Text = "未识别到语音。";
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    //var cancellation = result.Cancellation;
                    //ResultTextBox.Text = $"识别取消: {cancellation.Reason}";
                }
            }
        }
    }
}